
package java_jdbc;

//2：在控制台输入一个学生的全部信息，保存到数据库，
//注意日期的处理(Statement)
import java.sql.*;
import java.util.Scanner;

public class jdbc7 {

	public static void main(String[] args) {

		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("驱动没有找到");
			e.printStackTrace();
			System.exit(0);
		}
		String url = "jdbc:mysql://localhost:3306/java63?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
		Connection con = null;
		Statement st = null;

		try {
			con = DriverManager.getConnection(url, "root", "root");
			st = con.createStatement();
			Scanner input = new Scanner(System.in);
			System.out.println("请输入学生学号、姓名、性别、电话号、qq、微信、年级号、入学日期、城市、地址、生日、密码");
			int stuno = input.nextInt();
			String name = input.next();
			String sex = input.next();
			String tel = input.next();
			String qq = input.next();
			String wechat = input.next();
			String gradeid = input.next();
			String en = input.next();
			String formcity = input.next();
			String address = input.next();
			String birthday = input.next();
			String pass = input.next();
			String sql = "insert into s_student(stuno,sname,sex,telephone,qq,wechat,gradeid,enterdate,fromcity,address,birthday,pass)"
					+ "values(" + "'" + stuno + "'" + "," + "'" + name + "'" + "," + "'" + sex + "'" + "," + "'" + tel
					+ "'" + "," + "'" + qq + "'" + "," + "'" + wechat + "'" + "," + "'" + gradeid + "'" + ",'" + en
					+ "'," + "'" + formcity + "'" + "," + "'" + address + "'" + "," + "'" + birthday + "'" + ",'" + pass
					+ "')";
			System.out.println(sql);

			int r = st.executeUpdate(sql);
			//System.out.println(String.format("增加了%d条数据", r));
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {

			if (st != null)
				try {
					st.close();
				} catch (SQLException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			if (con != null)
				try {
					con.close();
				} catch (SQLException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
		}
	}
}

